package com.qbm.association.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qbm.association.entity.AdministratorS;
import com.qbm.association.entity.AdministratorT;
import com.qbm.association.entity.User;
import com.qbm.association.mapper.AdministratorSMapper;
import com.qbm.association.mapper.AdministratorTMapper;
import com.qbm.association.mapper.UserMapper;
import com.qbm.association.service.UserService;
import com.qbm.association.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: Aron
 * @date: 2021.12.10
 */
@Api(tags = "登录控制层")
@RestController
@RequestMapping("/association")
@CrossOrigin
public class LoginController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    AdministratorSMapper administratorSMapper;
    @Autowired
    AdministratorTMapper administratorTMapper;

    @ApiOperation(value = "管理员登录")
    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        String username=(String)para.get("user_name");
        String password=(String)para.get("password");
        System.out.println(password);
        HashMap<String,Object> hs=new HashMap<>();
        String pass= administratorSMapper.selectOne(new QueryWrapper<AdministratorS>().eq("id",username)).getPassword();
        System.out.println(pass);
        if(pass==null){
            hs.put("state",-1);
        }else{
            if(pass.equals(password)){
                String token= TokenUtil.sign(new User(username,password));
                hs.put("state",0);
                hs.put("token",token);
            }
            else {
                hs.put("state",1);
            }
        }
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/loginT",method = RequestMethod.POST)
    @ResponseBody
    public String loginT(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        String username=(String)para.get("user_name");
        String password=(String)para.get("password");
        System.out.println(password);

        HashMap<String,Object> hs=new HashMap<>();
        String pass= administratorTMapper.selectOne(new QueryWrapper<AdministratorT>().eq("id",username)).getPassword();
        System.out.println(pass);
        if(pass==null){
            hs.put("state",-1);
        }else{
            if(pass.equals(password)){
                String token= TokenUtil.sign(new User(username,password));
                hs.put("state",0);
                hs.put("token",token);
            }
            else {
                hs.put("state",1);
            }
        }
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}
